<template>
  <div class="songs-container">
    <div class="tab-bar">
      <span class="item" :class="{ active: tag == 0 }" @click="tag = 0"
        >全部</span
      >
      <span class="item" :class="{ active: tag == 7 }" @click="tag = 7"
        >华语</span
      >
      <span class="item" :class="{ active: tag == 96 }" @click="tag = 96"
        >欧美</span
      >
      <span class="item" :class="{ active: tag == 8 }" @click="tag = 8"
        >日本</span
      >
      <span class="item" :class="{ active: tag == 16 }" @click="tag = 16"
        >韩国</span
      >
    </div>
    <!-- 底部的table -->
    <table class="el-table playlit-table"  style=" background-image: linear-gradient(to bottom right, rgb(27,155,176) , rgb(8,59,79)); ">
      <thead>
        <th></th>
        <th></th>
        <th>音乐标题</th>
        <th>歌手</th>
        <th>专辑</th>
        <th>时长</th>
      </thead>
      <tbody style="">
        <tr
          class="el-table__row"
          v-for="(item, index) in songslist"
          :key="index"
        >
          <td>{{ index + 1 }}</td>
          <td>
            <div class="img-wrap">
              <img :src="item.album.picUrl" alt="" />
              <span class="iconfont icon-play" @click="player(item.id)"></span>
            </div>
          </td>
          <td>
            <div class="song-wrap">
              <div class="name-wrap">
                <!-- 音乐标题 -->
                <span>{{ item.name }}</span>
                <span class="iconfont icon-mv"></span>
              </div>
              <span>{{ item.alias[0] }}</span>
            </div>
          </td>
          <!--  歌手名 -->
          <td>{{ item.album.artists["0"].name }}</td>
          <!-- 专辑 -->
          <td>{{ item.album.name }}</td>
          <td>{{ item.duration }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
 import axios from "axios"; 
export default {
  name: "songs",
  data() {
    return {
      songslist: [],
      tag: "0"
    };
  },
  created() {
    axios({
      url: "https://autumnfish.cn/top/song",
      method: "get",
      params: {
        type: this.tag
      }
    }).then(res => {
     /*  console.log(res);  */
      this.songslist = res.data.data;
      //处理时长，毫秒转换成分
      for (let i = 0; i <this.songslist.length ;i++) {
        //获取每一个的总毫秒
        let duration = this.songslist[i].duration;
        //分: duration/1000/60
        //秒:duration/1000%60
        let min = parseInt(duration / 1000 / 60);
        if (min < 10) {
          min = "0" + min;
        }
        let sec = parseInt((duration / 1000) % 60);
        if (sec < 10) {
          sec = "0" + sec;
        }
        this.songslist[i].duration = min + ":" + sec;
      }
    });
  },
  watch: {
    tag() {
      axios({
        url: "https://autumnfish.cn/top/song",
        method: "get",
        params: {
          type: this.tag
        }
      }).then(res => {
        /*  console.log(res);  */
        this.songslist = res.data.data;
         /*  console.log(this.songslist);  */
        //处理时长，毫秒转换成分
        for (let i = 0; i <this.songslist.length; i++) {
          //获取每一个的总毫秒
          let duration = this.songslist[i].duration;
          //分: duration/1000/60
          //秒:duration/1000%60
          let min = parseInt(duration / 1000 / 60);
          if (min < 10) {
            min = "0" + min;
          }
          let sec = parseInt((duration / 1000) % 60);
          if (sec < 10) {
            sec = "0" + sec;
          }
          this.songslist[i].duration = min + ":" + sec;
        }
      });
    }
  },
  methods: {
    //播放歌曲
    player(id) {
      axios({
        url: "https://autumnfish.cn/song/url",
        method: "get",
        params: {
          id
        }
      }).then(res => {
        /* console.log(res) */
        console.log(res.data.data[0].url) 
        let musciurl = res.data.data[0].url;
        this.$store.state.musicUrl= musciurl;
       /*  console.log(this.$store.state.musicUrl+`~~~`) */
        /*console.log( this.$parent.musicUrl+`~~`) */
      });
    }
  }
};
</script>
<style >

</style>
